package com.kayt.cyberBar.common.base;

import com.kayt.cyberBar.common.PageData;
import com.kayt.cyberBar.common.dao.EntityDao;
import com.kayt.cyberBar.cyber.business.entity.po.BCyberInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Description
 *
 * @author Kevin
 * @since 12/26/2017 21:55
 */
public abstract class BaseDaoService<T> {

    @Autowired
    private EntityDao entityDao;
    private Class<T> entityClass;

    public BaseDaoService() {
        Type genType = getClass().getGenericSuperclass();
        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
        entityClass = (Class) params[0];
    }

    public Long save(T entity) throws SQLException {
        return entityDao.save(entity);
    }
    public int update(String hql, Object[] queryParams) throws SQLException{
        return entityDao.update(hql,queryParams);
    }
    public void batchDelete(List<T> list){
        entityDao.batchDelete(list);
    }
    public void batchInsert(List<T> list){
        entityDao.batchInsert(list);
    }
    public T getOne(String hql,Object[] objects) throws SQLException {
        return entityDao.getOne(hql,objects);
    }
    public List<T> getDataByHql(String hql,Object[] objects) throws SQLException {
        return entityDao.getDataByHql(-1,-1,hql,objects);
    }
    public List<T> getDataBySql(String sql,Object[] objects) throws SQLException {
        return entityDao.getPagingDataBySql(entityClass,-1,-1,sql,objects);
    }

    public int delete(T entity) {
        if (entity != null) {
            entityDao.deleteDetached(entity);
            return 1;
        }
        return 0;
    }

    public void update(T entity) throws SQLException {
        if (entity != null) {
            entityDao.update(entity);
        }
    }

    public T getEntity(long id) {
        if (id > 0) {
            return entityDao.findById(id, entityClass);
        }
        return null;
    }

    public PageData<T> findPage(int pageNum, int pageSize, String sql, Object[] params, LinkedHashMap<String, String> orderMap) throws SQLException {
        return entityDao.getPagingData(entityClass, pageNum, pageSize, sql, params, orderMap);
    }

    public List findListByHql(int pageNum, int pageSize, String hql, Object[] params) throws SQLException {
        return entityDao.getPagingDataByHql(pageNum, pageSize, hql, params);
    }

	public EntityDao getEntityDao() {
		return entityDao;
	}

	public void setEntityDao(EntityDao entityDao) {
		this.entityDao = entityDao;
	}
}
